package com.ning.activitys.skysound.fragment.musicpavilion;


import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.ning.activitys.skysound.R;
import com.ning.activitys.skysound.activity.MainActivity;
import com.ning.activitys.skysound.beans.GsonMusicListDetails;
import com.ning.activitys.skysound.beans.GsonRankingDetailsBeans;
import com.ning.activitys.skysound.service.PlayService;
import com.ning.activitys.skysound.volley.ReadStringRequestUtf;
import com.ning.activitys.skysound.volley.VolleySingleton;

/**
 * Created by dllo on 16/1/18.
 */
public class RankingDetailsFragment extends Fragment implements View.OnClickListener, AdapterView.OnItemClickListener {

    private String url;
    private ListView listView;
    private RankingDetailsAdapter recommendedAdapter;
    private MainActivity mainActivity;
    private ImageButton back;
    private GsonRankingDetailsBeans gsonRankingDetailsBeans;
    private GsonMusicListDetails gsonMusicListDetails;
    private Gson gson = new Gson();

    private int myPosition = 0;

    private volatile static RankingDetailsFragment rankingDetailsFragment;//单例的引用
    private int skySoundListType;

    //单例模式创建对象
    public static RankingDetailsFragment getInstance() {
        if (rankingDetailsFragment == null) {
            //线程锁,只有一个线程可以调用
            synchronized (RankingDetailsFragment.class) {
                if (rankingDetailsFragment == null) {
                    rankingDetailsFragment = new RankingDetailsFragment();
                }
            }
        }
        return rankingDetailsFragment;
    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mainActivity = (MainActivity) context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_ranking_details,null);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        back = (ImageButton) view.findViewById(R.id.btn_finish_ranking_details);
        listView = (ListView) view.findViewById(R.id.list_view_ranking_details);

        back.setOnClickListener(this);

        listView.setOnItemClickListener(this);

        getData();


    }



    private void getData(){
        url = getArguments().getString("Url");
        skySoundListType = mainActivity.playService.getSkySoundListType();

        //RequestQueue queue = Volley.newRequestQueue(mainActivity);

        ReadStringRequestUtf readStringRequestUtf = new ReadStringRequestUtf(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

                if (skySoundListType == PlayService.RANKING_TYPE) {

                    gsonRankingDetailsBeans = gson.fromJson(response, GsonRankingDetailsBeans.class);

                    recommendedAdapter = new RankingDetailsAdapter();
                    listView.setAdapter(recommendedAdapter);
                }
                if (skySoundListType == PlayService.MUSIC_LIST_TYPR) {

                    gsonMusicListDetails = gson.fromJson(response, GsonMusicListDetails.class);

                    recommendedAdapter = new RankingDetailsAdapter();
                    listView.setAdapter(recommendedAdapter);

                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

        VolleySingleton.getInstance()._addRequest(readStringRequestUtf);
    }

    @Override
    public void onClick(View v) {
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();

        transaction.detach(RankingDetailsFragment.getInstance());
        transaction.commit();

        //收回 rankingDetailsFragment 对象
        rankingDetailsFragment = null;


    }

    /**
     *  onItemClick
     * */
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        PlayService.IS_NET_MUSIC = 0;

        if (skySoundListType == PlayService.MUSIC_LIST_TYPR) {

            mainActivity.playService.setGsonMusicListDetails(gsonMusicListDetails);

            mainActivity.playService.playNet(gsonMusicListDetails.getSongs().get(position).getUrlList().get(0).getUrl(),position);
        }
    }


    /**
     *   内部类的适配器
     *
     * */
    public class RankingDetailsAdapter extends BaseAdapter {


        @Override
        public int getCount() {
            if (skySoundListType == PlayService.MUSIC_LIST_TYPR){

                return gsonMusicListDetails.getSongs().size();

            }if (skySoundListType == PlayService.RANKING_TYPE) {

                return gsonRankingDetailsBeans.getSongs().size();
            }
            return 0;
        }

        @Override
        public Object getItem(int position) {
            if (skySoundListType == PlayService.MUSIC_LIST_TYPR){
                return gsonMusicListDetails.getSongs().get(position);
            }
            if (skySoundListType == PlayService.RANKING_TYPE) {
                return gsonRankingDetailsBeans.getSongs().get(position);
            }
            return 0;
        }


        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if (convertView == null){
                convertView = LayoutInflater.from(mainActivity).inflate(R.layout.item_ranking_details,parent,false);
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);
            }else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            if (myPosition <3) {
                viewHolder.ranking.setTextSize(40);
                viewHolder.ranking.setTextColor(Color.RED);
                myPosition++;
            }
            viewHolder.ranking.setText((position+1)+"");

            if (skySoundListType == PlayService.MUSIC_LIST_TYPR){
                viewHolder.title.setText(gsonMusicListDetails.getSongs().get(position).getName());
                viewHolder.singer.setText(gsonMusicListDetails.getSongs().get(position).getSingerName());
            }
            if (skySoundListType == PlayService.RANKING_TYPE) {
                viewHolder.title.setText(gsonRankingDetailsBeans.getSongs().get(position).getTitle());
                viewHolder.singer.setText(gsonRankingDetailsBeans.getSongs().get(position).getSinger());
            }
            return convertView;
        }

        public class ViewHolder{
            private TextView ranking,title,singer;

            public ViewHolder(View view){
                ranking = (TextView) view.findViewById(R.id.tv_ranking_ranking_details);
                title = (TextView) view.findViewById(R.id.tv_title_ranking_details);
                singer = (TextView) view.findViewById(R.id.tv_singer_ranking_details);
            }
        }

    }
}
